<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="服务类目" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-decorator="['categoryParentId',validatorRules.categoryParentId]"
                    placeholder="请选择一级类目" @change="getCategoryList" style="width: 30%">
            <a-select-option v-for="item in categoryParentList"
                             :value="item.id"
                             :key="item.id">{{ item.categoryName }}
            </a-select-option>
          </a-select>
          <a-select v-decorator="['categoryId',validatorRules.categoryId]"
                    placeholder="请选择二级类目" :allowClear="true" style="width: 30%">
            <a-select-option v-for="item in categoryList"
                             :value="item.id"
                             :key="item.id">{{ item.categoryName }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="服务名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['serviceName', validatorRules.serviceName]" placeholder="请输入服务名称"></a-input>
        </a-form-item>
        <a-form-item label="副标题" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['subtitle']" placeholder="请输入副标题"></a-input>
        </a-form-item>
        <a-form-item label="图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-upload text="图片"
                    :number="6"
                    v-decorator="['image', {rules: [{ required: true, message: '请选择图片!', }]}]"
                    fileType="image"></j-upload>
        </a-form-item>
        <a-form-item label="图标" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-upload text="图标"
                    :number="1"
                    v-decorator="['icon',{rules: [{ required: true, message: '请选择图标!', }]}]"
                    fileType="image"></j-upload>
        </a-form-item>
        <a-form-item label="价格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['price', validatorRules.price]" placeholder="请输入价格" style="width: 30%"
                          :min="0"/>
        </a-form-item>
        <a-form-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-decorator="['specification',validatorRules.specification]"
                    placeholder="请选择规格" :allowClear="true" style="width: 30%">
            <a-select-option :value="0">次</a-select-option>
            <a-select-option :value="1">半天</a-select-option>
            <a-select-option :value="2">半-白天</a-select-option>
            <a-select-option :value="3">半-夜间</a-select-option>
            <a-select-option :value="4">全天</a-select-option>
            <a-select-option :value="5">月</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-radio-group v-decorator="['status', validatorRules.status]">
            <a-radio :value="0">
              上架
            </a-radio>
            <a-radio :value="1">
              下架
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['sort',validatorRules.sort]" placeholder="请输入排序" style="width: 30%" :min="0"/>
        </a-form-item>
        <a-form-item label="服务详情" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-editor v-decorator="['serviceDetails',{rules: [{ required: true, message: '请输入服务详情!', }]}]"
                    triggerChange
                    placeholder="请输入服务详情"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import {getAction, httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import {validateDuplicateValue} from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import JUpload from '@/components/jeecg/JUpload'
  import JEditor from '@/components/jeecg/JEditor'
  import ATextarea from "ant-design-vue/es/input/TextArea";


  export default {
    name: "MedicalEscortServiceItemsModal",
    components: {
      ATextarea,
      JDate,
      JUpload,
      JEditor
    },
    data() {
      return {
        form: this.$form.createForm(this),
        title: "操作",
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        confirmLoading: false,
        validatorRules: {
          sort: {
            rules: [{required: true, message: '请输入排序!'},]
          },
          status: {
            rules: [{required: true, message: '请选择状态!'},]
          },
          categoryParentId: {
            rules: [
              {required: true, message: '请选择服务类目!'},
            ]
          },
          categoryId: {
            rules: [
              {required: true, message: '请选择服务类目!'},
            ]
          },
          serviceName: {
            rules: [
              {required: true, message: '请输入服务名称!'},
            ]
          },
          image: {
            rules: [
              {required: true, message: '请上传图片!'},
            ]
          },
          icon: {
            rules: [
              {required: true, message: '请上传图标!'},
            ]
          },
          price: {
            rules: [
              {required: true, message: '请输入价格!'},
            ]
          },
          specification: {
            rules: [
              {required: true, message: '请选择规格!'},
            ]
          },
        },
        url: {
          add: "/escort/medicalEscortServiceItems/add",
          edit: "/escort/medicalEscortServiceItems/edit",
          getCategoryList: "/escort/medicalEscortServiceCategory/getCategorySelectList",
        },
        categoryParentList: [],
        categoryList: [],
      }
    },
    created() {
      this.getCategoryParentList();
    },
    methods: {
      getCategoryParentList() {
        getAction(this.url.getCategoryList, {level: 1}).then((res) => {
          if (res.success) {
            this.categoryParentList = res.result;
          }
        });
      },
      getCategoryList(value) {
        this.form.resetFields(['categoryId']);
        getAction(this.url.getCategoryList, {level: 2, parentId: value}).then((res) => {
          if (res.success) {
            this.categoryList = res.result;
          }
        });
      },
      add() {
        this.edit({});
      },
      edit(record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        if (record.id) {
          this.getCategoryList(record.categoryParentId)
        }
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'categoryId', 'status', 'categoryParentId', 'serviceName', 'subtitle', 'image', 'icon', 'price', 'sort', 'specification', 'serviceDetails', 'createBy', 'createTime', 'updateBy', 'updateTime', 'delFlag'))
        })
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据", formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel() {
        this.close()
      },
      popupCallback(row) {
        this.form.setFieldsValue(pick(row, 'categoryId', 'status', 'categoryParentId', 'serviceName', 'subtitle', 'image', 'icon', 'price', 'sort', 'specification', 'serviceDetails', 'createBy', 'createTime', 'updateBy', 'updateTime', 'delFlag'))
      },


    }
  }
</script>